﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.IO;
using System.Xml.Serialization;
using System.Globalization;

namespace PIT.Labs.SharePoint.Extensions
{

    /// <summary>
    /// This class contains "<a href="http://msdn.microsoft.com/en-us/library/bb383977%28v=vs.90%29.aspx">Extension Methods</a>" for the Hashtable class.
    /// </summary>
    /// <seealso cref="T:System.Collections.Hashtable"/>
    public static class HashtableExtension
    {
        /// <summary>
        /// Adds an element with the specified key and value into the Hashtable. The value is serialized as XML-String.
        /// </summary>
        /// <param name="hashtable">Represents a collection of key/value pairs that are organized based on the hash code of the key.</param>
        /// <param name="key">The key of the element to add.</param>
        /// <param name="value">The value of the element to add which is serialzes as XML-String. The value can be <b>Nothing</b>.</param>
        public static void AddAsXml(this Hashtable hashtable, Object key, Object value)
        {
            if (hashtable != null)
            {
                if (value == null)
                {
                    hashtable.Add(key, value);
                }
                else
                {
                    StringWriter sw = new StringWriter(CultureInfo.InvariantCulture);
                    try
                    {
                        XmlSerializer x = new XmlSerializer(value.GetType());
                        x.Serialize(sw, value);
                        sw.Flush();
                        hashtable.Add(key, sw.ToString());
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                    finally
                    {
                        sw.Dispose();
                    }
                }
            }
        }


        /// <summary>
        /// As a counterpart of the <see cref="PIT.Labs.SharePoint.Extensions.HashtableExtension.AddAsXml(Hashtable, Object, Object)"/> method, the value of the given key is deserialized.
        /// </summary>
        /// <typeparam name="T">The type of the value.</typeparam>
        /// <param name="hashtable">Represents a collection of key/value pairs that are organized based on the hash code of the key.</param>
        /// <param name="key">The key of the hashtable.</param>
        /// <returns>The deserialized value.</returns>
        public static T GetFromXml<T>(this Hashtable hashtable, Object key)
        {
            T result = default(T);
            if (hashtable == null || key == null)
                return result;
            else
            {
                if (hashtable.ContainsKey(key))
                {
                    String value = hashtable[key] as String;
                    if (!String.IsNullOrEmpty(value))
                    {
                        StringReader sr = new StringReader(value);
                        try
                        {
                            XmlSerializer x = new XmlSerializer(typeof(T));
                            result = (T)x.Deserialize(sr);
                        }
                        catch (Exception)
                        {
                            throw;
                        }
                        finally
                        {
                            sr.Dispose();
                        }
                    }
                }
            }
            return result;
        }
    }
}
